package com.woniuxy.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.entity.Address;
import com.woniuxy.entity.User;
import com.woniuxy.service.AddressService;

@RestController
@RequestMapping("/address")
public class AddressController {
	@Autowired
	private AddressService addressService;

	@RequestMapping("/find")
	public List<Address> find() {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");
		List<Address> addresses = addressService.findAddressByUid(user.getU_id());
		System.out.println(addresses);
		return addresses;

	}

	@RequestMapping("/add")
	public Boolean add(Address address) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");
		address.setU_id(user.getU_id());
		address.setType("n");
		return addressService.addAddress(address);

	}

	@RequestMapping("/type")
	public Boolean type(int a_id) {
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute("user");
		return addressService.updataAddressY(user.getU_id(), a_id);

	}

	@RequestMapping("/del")
	public Boolean del(int a_id) {
		return addressService.delAddress(a_id);

	}
}
